<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>装备详情</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="js/vue.js"></script>
    <script src="js/axios.min.js"></script>
    <style>
        body {
            background-color: #121212;
            color: #e0e0e0;
            font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            position: relative;
            min-height: 100vh;
        }

        .container {
            max-width: 1000px;
            margin: 80px auto;
            background-color: #1e1e1e;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 0 12px #222;
            min-height: 600px;
        }

        header h1 {
            margin-bottom: 24px;
            font-weight: 700;
            text-align: center;
            color: #aa9bf9;
        }

        .equipment-card {
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #2c2c2c;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.5);
        }

        .equipment-card img {
            width: 250px;
            height: auto;
            border-radius: 8px;
            margin-bottom: 20px;
        }

        .equipment-info {
            text-align: center;
        }

        .equipment-info h2 {
            margin-bottom: 10px;
            color: #ffffff;
        }

        .equipment-info p {
            margin: 4px 0;
            color: #cccccc;
            font-size: 16px;
        }

        .price {
            font-size: 20px;
            color: #ffd700;
            font-weight: bold;
        }

        .list-button {
            background-color: #ff9800;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }

        .list-button:hover {
            background-color: #e68900;
        }

        .listing-action {
            margin-top: 30px;
            width: 100%;
            text-align: center;
        }

        .listing-action input {
            padding: 10px;
            border-radius: 5px;
            width: 200px;
            margin-right: 10px;
            border: none;
            outline: none;
        }

        /* 关闭按钮样式 */
        .listing-wrapper {
            position: relative;
            width: 100%;
        }
        .close-btn {
            position: absolute;
            top: -400px;
            right: 0px;
            background: transparent;
            border: none;
            font-size: 28px;
            color: #ccc;
            cursor: pointer;
            transition: color 0.3s ease;
            user-select: none;
        }
        .close-btn:hover {
            color: #ff9800;
        }
    </style>
</head>
<body>

<main id="app">
    <section class="container">
        <header>
            <h1>饰品详情</h1>
        </header>

        <!-- 装备展示卡片 -->
        <div class="equipment-card" v-if="equipment.equipmentId">
            <img :src="equipment.equipmentImage" alt="装备图片">
            <div class="equipment-info">
                <h2>{{ equipment.equipmentName }}</h2>
                <p class="price">¥ {{ equipment.equipmentPrice }}</p>
                <p>类型: {{ equipment.equipmentType }}</p>
                <p>稀有度: {{ equipment.rarity }}</p>
                <p>库存: {{ equipment.stock }}</p>
                <p>磨损度: {{ equipment.exterior }}</p>
            </div>

            <!-- 上架区域，增加关闭按钮 -->
            <div class="listing-wrapper">
                <button class="close-btn" @click="goToUserPage">×</button>
                <div class="listing-action">
                    <input type="number" v-model="listingPrice" placeholder="设置上架价格">
                    <button class="list-button" @click="listItem">上架</button>
                </div>
            </div>
        </div>
    </section>
</main>

<script>
    new Vue({
        el: "#app",
        data: {
            equipment: {
                equipmentId: "",
                equipmentName: "",
                equipmentImage: "",
                equipmentPrice: "",
                equipmentType: "",
                rarity: "",
                stock: "",
                exterior: ""
            },
            listingPrice: "" // 上架价格
        },
        mounted() {
            this.getById();
        },
        methods: {
            getById() {
                const urlParams = new URLSearchParams(window.location.search);
                const id = urlParams.get("equipmentId");
                if (!id) {
                    alert("未提供装备 ID");
                    return;
                }
                axios.get("equipment/" + id)
                    .then(response => {
                        if (response.data.code === 200) {
                            this.equipment = response.data.data;
                        } else {
                            alert("装备信息加载失败");
                        }
                    })
                    .catch(error => {
                        console.error(error);
                        alert("请求出错");
                    });
            },
            listItem() {
                if (!this.listingPrice || this.listingPrice <= 0) {
                    alert("请输入有效的上架价格！");
                    return;
                }

                const userId = localStorage.getItem("userId");
                if (!userId) {
                    alert("请先登录再进行上架操作");
                    window.location.href = "login.html";
                    return;
                }

                const payload = {
                    equipmentId: this.equipment.equipmentId,
                    userId: parseInt(userId),
                    equipmentPrice: this.listingPrice,
                    status: 1, // 设置为已上架
                    createTime: new Date().toISOString()
                };

                axios.put("/ffub/equipment/update", payload)
                    .then(res => {
                        if (res.data && res.data.code === 200) {
                            alert("装备已成功上架！");
                            this.equipment.equipmentPrice = this.listingPrice;
                            // 上架成功跳转
                            window.location.href = "user.html";
                        } else {
                            alert("上架失败: " + (res.data.msg || "未知错误"));
                        }
                    })
                    .catch(() => {
                        alert("上架请求失败，请稍后重试");
                    });
            },
            goToUserPage() {
                window.location.href = "user.html";
            }
        }
    });
</script>

</body>
</html>
